package com.pub.functions;

import android.annotation.SuppressLint;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * 
 * @author zhuangzhonglong Date: 2007-1-29 Time: 14:09:03
 */
@SuppressLint("DefaultLocale")
public class StrFuncs {

	public static String byte2hex(int[] b) {
		StringBuffer hs = new StringBuffer();
		String stmp;
		if (b == null) {
			return "hex object null";
		}
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0xFF));
			if (stmp.length() == 1) {
				hs.append("0" + stmp + " ");
			} else {
				hs.append(stmp + " ");
			}
		}
		return hs.toString();
	}

	public static String byte2hex(byte[] b) {
		StringBuffer hs = new StringBuffer();
		String stmp;
		if (b == null) {
			return "hex object null";
		}
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0xFF));
			if (stmp.length() == 1) {
				hs.append("0" + stmp + " ");
			} else {
				hs.append(stmp + " ");
			}
		}
		return hs.toString();
	}

	public static String byte2hex(byte[] b, int len) {
		StringBuffer hs = new StringBuffer();
		String stmp;
		if (b == null) {
			return "hex object null";
		}
		for (int n = 0; n < b.length && n < len; n++, len--) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0xFF));
			if (stmp.length() == 1) {
				hs.append("0" + stmp + " ");
			} else {
				hs.append(stmp + " ");
			}
		}
		return hs.toString();
	}

	public static String nvl(Object o) {
		return o == null ? "" : o.toString();
	}

	public static String nvl(String s) {
		return s == null ? "" : s.trim();
	}

	public static <T> String join(T[] arr, String sep) {
		StringBuilder sb = new StringBuilder();
		int nCount = arr.length;
		for (int n = 0; n < nCount; n++) {
			sb.append(arr[n]);
			if (n < nCount - 1) {
				sb.append(sep);
			}
		}
		return sb.toString();
	}

	public static <T> String join(ArrayList<T> arr, String sep) {
		return join(arr.toArray(), sep);
	}

	public static boolean isEmpty(String s) {
		if (null == s)
			return true;
		else if ("null".equals(s.trim().toLowerCase()))
			return true;
		else
			return s.trim().length() == 0;
	}
	
	public static boolean isLocationEmpty(String s) {
		if (null == s)
			return true;
		else if ("".equals(s.trim().toLowerCase())){
			return true;
		} else if ("null".equals(s.trim().toLowerCase()))
			return true;
		else
			return s.trim().length() == 0;
	}

	public static boolean notEmpty(String s) {
		return s != null && s.trim().length() > 0;
	}

	public static Double toDouble(String s) {
		if (isEmpty(s))
			return null;
		return Double.valueOf(s);
	}

	public static BigDecimal toBitDecimal(String s) {
		if (isEmpty(s))
			return null;
		return BigDecimal.valueOf(Double.valueOf(s));
	}

	public static String valueOf(Double value) {
		if (value == null)
			return null;
		return String.valueOf(value);
	}

	public static String valueOf(BigDecimal value) {
		if (value == null)
			return null;
		return String.valueOf(value);
	}

	public static String quotedStr(String str) {
		if (str == null)
			return "''";
		return "'" + str.replace("'", "''") + "'";
	}

	public static String join(List<?> strs, String sep) {
		String result = "";
		if (strs == null)
			return result;
		for (int n = 0; n < strs.size(); n++) {
			if (n > 0)
				result += sep;
			result += strs.get(n);
		}
		return result;
	}

	public static Integer toInteger(String s) {
		if (isEmpty(s))
			return null;
		return Integer.valueOf(s);
	}

	public static Integer toInteger(String s, int defValue) {
		try {
			return Integer.valueOf(s);
		} catch (Throwable e) {
			return defValue;
		}
	}

	public static String getCommonPrefix(String s1, String s2) {
		if (s1 == null || s2 == null)
			return "";
		StringBuilder sb = new StringBuilder("");
		for (int n = 0; n < Math.min(s1.length(), s2.length()); n++) {
			char c = s1.charAt(n);
			if (c != s2.charAt(n))
				break;
			sb.append(c);
		}
		return sb.toString();
	}

	public static Long toLong(String s) {
		if (isEmpty(s))
			return null;
		return Long.valueOf(s);
	}

	public static Long toLong(String s, Long defValue) {
		if (isEmpty(s))
			return defValue;
		return Long.valueOf(s);
	}

	public static String valueOf(Integer value) {
		if (value == null)
			return null;
		return String.valueOf(value);
	}

	public static String valueOf(Object o) {
		if (o == null)
			return "";
		return "" + o;
	}

	public static String left(String s, int cCount) {
		return s.substring(0, Math.min(s.length(), cCount));
	}

	public static String left(String s, int lift, int right) {
		return s.substring(lift, Math.min(s.length(), right));
	}
	
	public static boolean isAllNumber(String s) {
		return s.matches("\\d+");
	}

	public static String ellipseText(Object oText, int maxBytes) {
		if (oText == null)
			return "";
		return ellipseText(oText.toString(), maxBytes);
	}

	public static String ellipseText(String text, int maxBytes) {
		if (maxBytes >= text.length() * 2)
			return text;
		String subText = text.substring(0, Math.min(maxBytes, text.length()));
		try {
			byte[] bts = subText.getBytes("gbk");
			if (maxBytes < bts.length) { // found HZ
				String s = new String(bts, 0, maxBytes, "gbk");
				String s1 = new String(bts, 0, maxBytes + 1, "gbk");
				if (s1.length() == s.length())
					s = new String(bts, 0, maxBytes - 1);

				return s + "...";
			}
		} catch (Exception e) {
			// never fail, do nothing
		}
		if (subText.length() < text.length())
			subText += "...";
		return subText;
	}

	public static String emptyIsZero(String s) {
		return s == null ? "0" : (s.length() == 0 ? "0" : s);
	}

	public static String anyLike(String s) {
		return "%" + s.trim() + "%";
	}

	public static String rightLike(String s) {
		return s + "%";
	}

	public static String anyLikeIfNotEmpty(String s) {
		if (isEmpty(s))
			return s;
		return anyLike(s);
	}

	public static String rightLikeIfNotEmpty(String s) {
		if (isEmpty(s))
			return s;
		return rightLike(s);
	}

	public static boolean equalsIgnoreNull(String s1, String s2) {
		if (s1 == null)
			s1 = "";
		if (s2 == null)
			s2 = "";
		return s1.equals(s2);
	}

	/**
	 * add by 潘韶华 去空格
	 * 
	 * @param s
	 * @return
	 */
	public static String trim(String s) {
		if (isEmpty(s))
			return s;
		return s.trim();
	}

	/**
	 * 判断字符串是否为NULL或等于某个特定值，在检测下拉框值时十分好用。 空时返回true
	 * 
	 * @param str
	 *            字符串
	 * @param empty
	 *            特定值
	 * @return true/false
	 */
	public static boolean CheckString(String str, String empty) {
		if (str == null || str.equals(empty)) {
			return true;
		} else {
			return false;
		}
	}

	public static String getSplitStr(String code, boolean isUpper) {
		String series = "";
		if (StrFuncs.notEmpty(code)) {
			code = code.replace("\r", " ");
			code = code.replace("\n", " ");
			String seriesNum[] = code.split(" ");
			StringBuilder builder = new StringBuilder();
			for (int i = 0; i < seriesNum.length; i++) {
				if (StrFuncs.notEmpty(seriesNum[i])) {
					if (isUpper)
						builder.append("'" + seriesNum[i].trim().toUpperCase() + "',");
					else
						builder.append("'" + seriesNum[i].trim() + "',");
				}
			}
			series = builder.toString();
			if (StrFuncs.notEmpty(series))
				series = series.substring(0, series.length() - 1);
			return series;
		}
		return null;
	}

	public static String getInFields(String[] fieldValues) {
		StringBuffer sb = new StringBuffer();
		if (fieldValues != null) {
			for (String value : fieldValues) {
				sb.append("'" + value + "',");
			}
		}
		sb.append("'-1'");
		return sb.toString();
	}

	public static String getInFields(Long[] fieldValues) {
		StringBuffer sb = new StringBuffer();
		if (fieldValues != null) {
			for (Long value : fieldValues) {
				sb.append("'" + value + "',");
			}
		}
		sb.append("'-1'");
		return sb.toString();
	}

	public static boolean compareAandB(String a, String b) {
		if (a == null && b == null) {
			return true;
		}
		if (a != null) {
			if (a.equals(b)) {
				return true;
			}
		}

		return false;
	}

}
